/*
 * BibliotecaEditLocationFrame.java
 *
 * Created on 29 settembre 2008, 17.48
 */
package it.biblioteca;


import it.biblioteca.util.ComboFields;
import it.biblioteca.util.UtilFileOperation;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;

/**
 *
 * @author  giovine
 */
public class BibliotecaEditLocationFrame extends javax.swing.JFrame {

    ComboFields my_fields;
    ArrayList<String> list;
    DefaultListModel lm;
    BibliotecaAddBooksFrame super_frame;
    BibliotecaManagementFrame super_management;
    
    boolean add;

    /** Creates new form BibliotecaEditLocationFrame */
    public BibliotecaEditLocationFrame(ComboFields f, BibliotecaAddBooksFrame s,boolean add) {
        this.super_frame = (BibliotecaAddBooksFrame)s;
        this.my_fields = f;
        list = this.my_fields.getFields();
        lm = new DefaultListModel();
        this.add = add;
        initComponents();
    }
    /** Creates new form BibliotecaEditLocationFrame */
    public BibliotecaEditLocationFrame(ComboFields f, BibliotecaManagementFrame s,boolean add) {
        this.super_management = (BibliotecaManagementFrame)s;
        this.my_fields = f;
        list = this.my_fields.getFields();
        lm = new DefaultListModel();
        this.add = add;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        location_list = new javax.swing.JList();
        close_bt = new javax.swing.JButton();
        new_location_tf = new javax.swing.JTextField();
        add_bt = new javax.swing.JButton();
        remove_bt = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BiblioTeCa - Modifica locazioni");
        setIconImage(new ImageIcon("files/icon_desktop.png").getImage());
        setResizable(false);
        setUndecorated(true);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 0, 18));
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("Modifica le locazioni dei libri.");

        Iterator i = list.iterator();

        while(i.hasNext()){
            String s = (String) i.next();
            System.out.println("Elemento estratto: "+s);
            lm.addElement(s);
        }

        this.location_list.setModel(lm);
        jScrollPane2.setViewportView(location_list);

        close_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_close.png"))); // NOI18N
        close_bt.setLabel("Chiudi");
        close_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close_btActionPerformed(evt);
            }
        });

        add_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_go.png"))); // NOI18N
        add_bt.setText("Aggiungi");
        add_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                add_btActionPerformed(evt);
            }
        });

        remove_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_clean.png"))); // NOI18N
        remove_bt.setText("Rimuovi");
        remove_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remove_btActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(remove_bt)
                        .addGap(20, 20, 20))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(new_location_tf)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                        .addComponent(close_bt)
                        .addGap(37, 37, 37))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(add_bt)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(84, 84, 84)
                .addComponent(jLabel1)
                .addContainerGap(108, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(add_bt)
                    .addComponent(new_location_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(226, 226, 226)
                        .addComponent(close_bt))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(remove_bt)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void close_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close_btActionPerformed
    UtilFileOperation.saveFields(this.my_fields);   
    
    dispose();
}//GEN-LAST:event_close_btActionPerformed

private void remove_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remove_btActionPerformed
    int index = location_list.getSelectedIndex();
    
    this.list.remove(index);
    this.my_fields.setFields(list);
    
    Object remove = lm.remove(index);
    int size = lm.getSize();
    if (size == 0) { //Nobody's left, disable firing.
        this.remove_bt.setEnabled(false);

    } else { //Select an index.
        if (index == lm.getSize()) {
            //removed item in last position
            index--;
        }
    }
    this.location_list.setModel(lm);
    if(add)
        this.super_frame.removeComboItem();
}//GEN-LAST:event_remove_btActionPerformed

private void add_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add_btActionPerformed
    lm.addElement(this.new_location_tf.getText());
    this.location_list.setModel(lm);
    
    list.add(this.new_location_tf.getText());
    this.my_fields.setFields(list);
    
    if(add)
        this.super_frame.addComboItem(this.new_location_tf.getText());
    
    this.new_location_tf.setText("");
}//GEN-LAST:event_add_btActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton add_bt;
    private javax.swing.JButton close_bt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList location_list;
    private javax.swing.JTextField new_location_tf;
    private javax.swing.JButton remove_bt;
    // End of variables declaration//GEN-END:variables
}
